package cn.edu.fzu.homemaking.sso.service.impl;

import java.io.IOException;

import cn.edu.fzu.homemaking.sso.service.DingDingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import cn.edu.fzu.homemaking.common.SignValidator;
import cn.edu.fzu.homemaking.helper.ShutDownHelper;
import cn.edu.fzu.homemaking.io.DingDingMessage;
import cn.edu.fzu.homemaking.io.OgReq;

/**
 * 钉钉机器人对接业务
 * 
 * @author fangxin
 * @date 2022/11/16
 */
@Service("dingDingService")
public class DingDingServiceImpl implements DingDingService {

    private static final Logger          log           = LoggerFactory.getLogger(DingDingServiceImpl.class);

    private static final String          TITLE         = "家政系统平台";
    private static final DingDingMessage USAGE         = DingDingMessage.build(TITLE, "帮助说明", null,
            "执行远程命令 remoteCmd:ip=;username=;password=;cmd=",
            "部署SSO模块前端页面: deploy sso fronted",
            "部署SSO模块后端: deploy sso java",
            "部署user前端: deploy user fronted",
            "部署user后端：deploy user java",
            "部署wallet：deploy wallet",
            "部署company前端: deploy company fronted");

    private final SignValidator          signValidator = SignValidator.homeMakingInfo();

    @Value("${login.url}")
    private String                       indexUrl;

    @Override
    public DingDingMessage query(OgReq req, String timestamp, String sign) {

        if (!signValidator.validate(timestamp, sign)) {
            log.warn("sign not match, req={}, timestamp={}, sign={}", JSON.toJSONString(req), timestamp, sign);
            return null;
        }

        String command = req.getText().getContent().trim();

        if (command.startsWith("remoteCmd:")) {
            return deploy(command.substring(10));
        }
        if (command.startsWith("deploy sso fronted")) {
            return deploySsoFronted();
        }
        if (command.startsWith("deploy sso java")) {
            return deploySsoJava();
        }
        if (command.startsWith("deploy wallet")) {
            return deployWallet();
        }
        if (command.startsWith("deploy user fronted")) {
            return deployUserFronted();
        }
        if (command.startsWith("deploy user java")) {
            return deployUserJava();
        }
        if (command.startsWith("deploy company fronted")) {
            return deployCompanyFronted();
        }

        return USAGE;
    }


//    private DingDingMessage deploySsoFronted() {
//        String commend = "/home/ubuntu/deploySso.sh &";
//        String deploy = ShutDownHelper.deploy(commend);
//        if (deploy != null) {
//            return DingDingMessage.build(TITLE, "SSO前端部署失败", null, deploy);
//        }
//        return DingDingMessage.build(TITLE, "SSO前端部署成功", null, indexUrl);
//
//    }
    public static DingDingMessage deploySsoFronted() {
        String commend = "sh /home/ubuntu/deploySso.sh";
        try {
            ShutDownHelper.execute("ubuntu", "76639406zyp.", "101.43.44.19", commend);
        } catch (IOException e) {
            return DingDingMessage.build(TITLE, "SSO前端部署失败", null, e.getMessage());
        }
        //        return DingDingMessage.build(TITLE, "home-user前端部署成功", null, indexUrl);
        return null;
    }

    //部署sso后端
    public static DingDingMessage deploySsoJava() {
        String commend = "sh /home/ubuntu/deploySsoJava.sh";
        try {
            ShutDownHelper.execute("ubuntu", "76639406zyp.", "101.43.44.19", commend);
        } catch (IOException e) {
            return DingDingMessage.build(TITLE, "SSO后端部署失败", null, e.getMessage());
        }
        //        return DingDingMessage.build(TITLE, "home-user前端部署成功", null, indexUrl);
        return null;
    }
    public static DingDingMessage deployWallet() {
        String commend = "sh /home/ubuntu/deployWalletJava.sh";
        try {
//            ShutDownHelper.execute("root", "Aa242318.", "8.130.52.6", commend);
            ShutDownHelper.execute("ubuntu", "76639406zyp.", "101.43.44.19", commend);
        } catch (IOException e) {
            return DingDingMessage.build(TITLE, "0", null, e.getMessage());
        }
//                return DingDingMessage.build(TITLE, "1", null, indexUrl);
        return null;
    }
    
    public static DingDingMessage deployCompanyFronted() {
        String commend = "sh /home/ubuntu/deployCompany.sh";
        try {
//            ShutDownHelper.execute("root", "Aa242318.", "8.130.52.6", commend);
            ShutDownHelper.execute("ubuntu", "76639406zyp.", "101.43.44.19", commend);
        } catch (IOException e) {
            return DingDingMessage.build(TITLE, "0", null, e.getMessage());
        }
//                return DingDingMessage.build(TITLE, "1", null, indexUrl);
        return null;
    }


    public static DingDingMessage deployUserFronted() {
        String commend = "sh /root/deployUser.sh";
        try {
            ShutDownHelper.execute("root", "Aa242318", "8.130.52.6", commend);
        } catch (IOException e) {
            return DingDingMessage.build(TITLE, "home-user前端部署失败", null, e.getMessage());
        }
        //        return DingDingMessage.build(TITLE, "home-user前端部署成功", null, indexUrl);
        return null;
    }


    private DingDingMessage deployUserJava() {
        String commend = "sh /root/deployUserJava.sh";
        try {
            ShutDownHelper.execute("root", "Aa242318", "8.130.52.6", commend);
        } catch (IOException e) {
            return DingDingMessage.build(TITLE, "home-user后端部署失败", null, e.getMessage());
        }
        return DingDingMessage.build(TITLE, "home-user前端部署成功", null, indexUrl);

    }


    /** 系统部署 **/
    private DingDingMessage deploy(String msg) {
        String[] split = msg.split(";");
        String ip = null;
        String username = null;
        String password = null;
        String cmd = null;
        for (String str : split) {
            if (str.startsWith("ip=")) {
                ip = str.substring(3);
                continue;
            }
            if (str.startsWith("username=")) {
                username = str.substring(9);
            }
            if (str.startsWith("password=")) {
                password = str.substring(9);
            }
            if (str.startsWith("cmd=")) {
                cmd = str.substring(4);
            }
        }
        if (ip == null || username == null || password == null || cmd == null) {
            return DingDingMessage.build(TITLE, "请填写正确的参数", null, "ip=", "username=", "password=", "cmd=");
        }
        try {
            ShutDownHelper.execute(username, password, ip, cmd);
        } catch (IOException e) {
            return DingDingMessage.build(TITLE, "命令执行失败", null, e.getMessage());
        }
        return DingDingMessage.build(TITLE, "命令执行成功", null, "");
    }
}
